<template>
  <div class="index">
    <!-- 标题 -->
    <module-title :title="moduleTitle"></module-title>

    <!-- 帐号上传 start -->
    <el-card style="margin-top: 20px;">
      <div class="card-title">账号上传</div>
      <div>
        <!-- 列表 -->
        <div class="list">
          <div class="list-header">
            <span
              style="width: 25%; border-right: 1px solid #d9d9d9; font-weight: 700; color: #1074fd"
              >地区</span
            >
            <span
              style="width: 25%; border-right: 1px solid #d9d9d9; font-weight: 700; color: #1074fd"
              >所属APP</span
            >
<!--            <span-->
<!--              style="width: 25%; border-right: 1px solid #d9d9d9; font-weight: 700; color: #1074fd"-->
<!--              >账号性别</span-->
<!--            >-->
            <span
              style="width: 25%; border-right: 1px solid #d9d9d9; font-weight: 700; color: #1074fd"
              >所属行业</span
            >
            <span
              style="width: 26%; border-right: 1px solid #d9d9d9; font-weight: 700; color: #1074fd"
              >上传记录</span
            >
            <span style="width: 24%; font-weight: 700; color: #1074fd"
              >上传时间</span
            >
          </div>
          <div v-if="list.length > 0">
            <div class="list-body" v-for="item in list" :key="item.id">
              <span
                style="width: 25%; border-right: 1px solid #d9d9d9; color: #4d4d4d; font-size: 14px"
                >{{ item.countryName }}</span
              >
              <span
                style="width: 25%; border-right: 1px solid #d9d9d9; color: #4d4d4d; font-size: 14px"
                >{{ item.appName }}</span
              >
<!--              <span-->
<!--                style="width: 25%; border-right: 1px solid #d9d9d9; color: #4d4d4d; font-size: 14px"-->
<!--              >-->
<!--                <span v-if="item.sexType === '3'">全部</span>-->
<!--                <span v-if="item.sexType === '1'">男性</span>-->
<!--                <span v-if="item.sexType === '2'">女性</span>-->
<!--              </span>-->
              <span
                style="width: 25%; border-right: 1px solid #d9d9d9; color: #4d4d4d; font-size: 14px"
                >{{ item.industryName }}</span
              >
              <span
                style="width: 26%; border-right: 1px solid #d9d9d9; color: #4d4d4d; font-size: 14px"
              >
                <span v-if="item.status === '1'" style="color: #1074fd"
                  >成功</span
                >
                <span v-else style="color: red">失败</span>
              </span>
              <span style="width: 24%; color: #4d4d4d; font-size: 14px">
                {{ item.uploadTime.substring(0, item.uploadTime.length - 2) }}
              </span>
            </div>
          </div>
          <div class="noneList" v-else>当前暂无行业数据</div>
        </div>
      </div>
      <!-- 新增按钮 -->
      <div style="margin-top: 20px; text-align: right ">
        <el-button class="add-button" @click="addDialog = true">新增1</el-button>
      </div>

      <!-- 分页 -->
      <div>
        <div style="text-align: center">
          <el-pagination
            :page-size="6"
            :pager-count="5"
            layout="total, prev, pager, next"
            :total="total"
            @prev-click="paginationPrevClick"
            @next-click="paginationNextClick"
            @current-change="paginationNextChange"
          ></el-pagination>
        </div>
      </div>
    </el-card>
    <!-- 账号上传 end -->

    <!-- 弹窗 -->
    <!-- 新增数据 start -->
    <el-dialog title="新增数据" center :visible.sync="addDialog" width="40%">
      <div style="margin-top:18px">
        <span style="margin-left: 28px">地区：</span>
        <el-select
          v-model="languageValue"
          placeholder="语言"
          style="display: inline-block;width: 130px;margin-right: 20px"
          @change="languageChange"
        >
          <el-option
            v-for="item in languageList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="cityValue"
          placeholder="国家"
          style="display: inline-block;width: 130px"
        >
          <el-option
            v-for="item in cityList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div style="margin-top:18px">
        <span>关键词：</span>
        <el-input v-model="uploadData.keyWord" placeholder="请输入关键词"></el-input>
      </div>
      <div style="margin-top:18px">
        <span>APP名称：</span>
        <el-select v-model="appValue" placeholder="请选择">
          <el-option
            v-for="item in appList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
<!--      <div style="margin-top:18px">-->
<!--        <span>账号性别：</span>-->
<!--        <el-select v-model="sexValue" placeholder="请选择" style="width: 100px">-->
<!--          <el-option-->
<!--            v-for="item in sexList"-->
<!--            :key="item.value"-->
<!--            :label="item.label"-->
<!--            :value="item.value"-->
<!--          >-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </div>-->
      <div style="margin-top:18px">
        <span>所属行业：</span>
        <el-select v-model="tradeValue" placeholder="请选择">
          <el-option
            v-for="item in tradeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div id="updBox" style="display: flex; margin-top: 18px;">
        <span style="margin-top: 9px">上传文档：</span>
        <el-upload
          ref="uploadForm"
          :headers="headers"
          :data="uploadData"
          action="http://qk-api.aike.info/qk/account/addAccount"
          :auto-upload="false"
          :on-success="docUploadSuccess"
          :limit="1"
          accept=".txt"
        >
          <el-input style="width: 320px; caret-color: transparent"></el-input>
        </el-upload>
      </div>
      <div style="color: red;margin: 10px">请上传TXT文件(文件格式为一行一个账号)</div>
      <div class="button">
        <el-button class="button-cancel" @click="addCancel()">取消</el-button>
        <el-button class="button-comfirm" @click="addComfirm()">确认</el-button>
      </div>
    </el-dialog>
    <!-- 新增数据 end -->
  </div>
</template>

<script>
import { createSign } from "@/utils/sign";
import API from "@/network/api/emsApi";
import ModuleTitle from "@/components/content/ModuleTitle.vue";
export default {
  name: "index",
  data() {
    return {
      moduleTitle: "上传数据",
      addDialog: false, // 打开新增弹窗
      appValue: "", // 所属app
      languageValue: "",
      cityValue: "",
      // sexValue: 3,
      tradeValue: 1, // 所属行业
      total: 0,
      currentPage: 1, // 当前页
      list: [], // 数据列表
      // 请求头
      headers: {
        session_id: ""
      },
      // 新增数据参数
      uploadData: {
        appId: "",
        industryId: "",
        organId: 0,
        timestamp: "",
        languageId: "",
        countryId: "",
        keyWord: ""
      },
      // 查询数据参数
      queryForm: {
        pageNum: 1,
        pageSize: 6
      },
      // 公共参数
      publicForm: {
        timestamp: ""
      },
      sexList: [
        {
          label: "全部",
          value: 3
        },
        {
          label: "男性",
          value: 1
        },
        {
          label: "女性",
          value: 2
        }
      ],
      cityList: [],
      languageList: [],
      appList: [], // app列表
      tradeList: [] // 行业列表
    };
  },
  created() {
    this.currentUserInfo = JSON.parse(localStorage.getItem("updateUserInfo"));
    this.headers.session_id = this.currentUserInfo.token;
    this.getAppTradeInfo();
    this.queryRecord();
    this.queryLanguage();
  },
  methods: {
    /**
     * 逻辑相关
     */
    // 取消新增
    addCancel() {
      this.addDialog = false; // 关闭弹窗
      this.appValue = this.tradeValue = ""; // 初始化新增信息
      this.$refs.uploadForm.clearFiles(); // 清空已上传文件
    },

    // 确认新增
    addComfirm() {
      if (this.appValue === "") {
        this.$message.warning("请选择APP名称");
      } else if (this.tradeValue === "") {
        this.$message.warning("请选择所属行业");
      } else if (this.languageValue === "") {
        this.$message.warning("请选择语言");
      } else if (this.cityValue === "") {
        this.$message.warning("请选择国家");
      } else {
        this.addTrade();
      }
    },

    // 分页
    paginationPrevClick(page) {
      this.currentPage = page;
      this.queryForm.pageNum = page;
      this.queryRecord();
    },
    paginationNextClick(page) {
      this.currentPage = page;
      this.queryForm.pageNum = page;
      this.queryRecord();
    },
    paginationNextChange(page) {
      this.currentPage = page;
      this.queryForm.pageNum = page;
      this.queryRecord();
    },

    /**
     * 网络请求
     */
    async languageChange() {
      console.log(this.languageValue, "languageValue");
      const singTime = {
        timestamp: +new Date(),
        languageId: this.languageValue + ""
      };
      const sign = createSign(
        JSON.stringify({
          ...singTime
        })
      );
      const citySearch = {
        timestamp: singTime.timestamp,
        sign: sign,
        languageId: singTime.languageId + ""
      };
      await API.queryCountry({
        ...citySearch
      }).then(res => {
        this.cityList = [];
        this.cityValue = "";
        for (let i = 0; i < res.data.data.length; i++) {
          let cityCurrent = {};
          cityCurrent.value = res.data.data[i].id;
          cityCurrent.label = res.data.data[i].name;
          this.cityList.push(cityCurrent);
        }
      });
    },
    // 语言
    async queryLanguage() {
      const singTime = {
        timestamp: +new Date()
      };
      const sign = createSign(
        JSON.stringify({
          ...singTime
        })
      );
      const languageSearch = {
        timestamp: singTime.timestamp,
        sign: sign
      };
      await API.queryLanguage({
        ...languageSearch
      }).then(res => {
        for (let i = 0; i < res.data.data.length; i++) {
          let cityCurrent = {};
          cityCurrent.value = res.data.data[i].id;
          cityCurrent.label = res.data.data[i].name;
          this.languageList.push(cityCurrent);
        }
      });
    },
    // 查询app和企业
    async getAppTradeInfo() {
      this.publicForm.timestamp = +new Date();
      if (this.publicForm.sign) {
        delete this.publicForm.sign;
      }
      this.publicForm.sign = createSign(
        JSON.stringify({
          ...this.publicForm
        })
      );
      await API.getAppTradeInfo({
        ...this.publicForm
      }).then(res => {
        if (res.data.code === 0) {
          let arr = res.data.data;
          for (let i = 1; i < arr.appList.length; i++) {
            let temp = {};
            temp.value = arr.appList[i].id;
            temp.label = arr.appList[i].name;
            this.appList.push(temp);
          }
          for (let k in arr.industryList) {
            let temp = {};
            temp.value = arr.industryList[k].id;
            temp.label = arr.industryList[k].name;
            this.tradeList.push(temp);
          }
          // const item = {
          //   label: '全部',
          //   value: 0
          // }
          // this.tradeList.unshift(item)
        } else {
          this.$message.warning(res.data.msg);
        }
      });
    },

    // 新增数据
    addTrade() {
      this.uploadData.organId = this.currentUserInfo.organId + "";
      this.uploadData.appId = String(this.appValue);
      this.uploadData.industryId = String(this.tradeValue);
      this.uploadData.timestamp = +new Date();
      this.uploadData.languageId = String(this.languageValue);
      this.uploadData.countryId = String(this.cityValue);
      // this.uploadData.sexType = String(this.sexValue);
      if (this.uploadData.sign) {
        delete this.uploadData.sign;
      }
      console.log(this.uploadData, "                                                                                                                       this.uploadData");
      this.uploadData.sign = createSign(
        JSON.stringify({
          ...this.uploadData
        })
      );
      this.$refs.uploadForm.submit();
    },
    docUploadSuccess(res) {
      if (res.code === 0) {
        this.$message.success("上传成功");
        // location.reload();
      } else {
        this.$message.warning(res.msg);
      }
      this.addDialog = false;
      // this.queryRecord();
    },

    // 查询数据
    async queryRecord() {
      this.queryForm.organId = this.currentUserInfo.organId;
      this.queryForm.pageNum = this.currentPage;
      this.publicForm.timestamp = +new Date();
      if (this.publicForm.sign) {
        delete this.publicForm.sign;
      }
      this.publicForm.sign = createSign(
        JSON.stringify({
          ...this.publicForm,
          ...this.queryForm
        })
      );
      await API.getRecordQuery({
        ...this.publicForm,
        ...this.queryForm
      }).then(res => {
        if (res.data.code === 0) {
          this.list = res.data.data.records;
          this.total = res.data.data.total;
        } else {
          this.$message.warning(res.data.msg);
        }
      });
    }
  },
  components: { ModuleTitle }
};
</script>

<style scoped>
.card-title {
  color: #1074fd;
  font-size: 18px;
  margin-bottom: 14px;
}
.list {
  position: relative;
  width: 100%;
  height: 356px;
  border: 1px solid #d9d9d9;
}
.list-header {
  display: flex;
  height: 50px;
  background-color: #f2f2f2;
  border-bottom: 1px solid #d9d9d9;
  text-align: center;
  line-height: 50px;
}
.list-body {
  display: flex;
  height: 50px;
  border-bottom: 1px solid #d9d9d9;
  text-align: center;
  line-height: 50px;
}
.add-button {
  background-color: #1074fd;
  color: #ffffff;
  border: 1px solid #1074fd;
}

.button {
  margin-top: 100px;
  text-align: right;
}
.button-cancel {
  color: #4d4d4d;
  font-size: 14px;
  border-radius: 4px;
  border: 1px solid #cccccc;
}
.button-comfirm {
  color: #ffffff;
  font-weight: bold;
  font-size: 14px;
  border-radius: 4px;
  background: #1074fd;
}

/deep/ #updBox .el-upload-list__item-name {
  margin-top: 3px;
}

/deep/ #updBox .el-upload-list__item:first-child {
  margin-top: -34px;
}

/deep/ #updBox .el-upload-list {
  padding: 0 10px;
}

.noneList {
  position: relative;
  width: 100%;
  height: 300px;
  margin: 0 auto 40px;
  text-align: center;
  line-height: 300px;
  font-size: 30px;
}
</style>
